/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
const levelOrder = function (root) {
  const res = []
  if (root == null) return res
  let next = [root]
  while (next.length > 0) {
    next = tr(res, next)
  }
  return res
}

function tr(res, nodeArr) {
  const arr = []
  const nextLevelNodes = []
  for (let i = 0; i < nodeArr.length; i++) {
    arr.push(nodeArr[i].val)
    if (nodeArr[i].left) {
      nextLevelNodes.push(nodeArr[i].left)
    }
    if (nodeArr[i].right) {
      nextLevelNodes.push(nodeArr[i].right)
    }
  }
  if (arr.length) res.push(arr)
  return nextLevelNodes
}
